﻿using Transformable_Engine_v2.Engine.Input;

namespace Project_Maverick.Controller.Input
{
    public class InputManager
    {
        public delegate void OnInputReceivedHandler(PlayerInputCommand playerInputCommand);
        public event OnInputReceivedHandler OnInputReceived;

        private readonly PlayerInput _playerInput;

        public InputManager()
        {
            _playerInput = PlayerInput.FromFile("Content/Config/Input.cfg");

            KeyboardHandler.OnKeyboardPress += LocalInputKeyPress;
            KeyboardHandler.OnKeyboardReleased += LocalInputKeyReleased;
        }

        private void LocalInputKeyReleased(KeyboardInput input)
        {
            if (_playerInput.Actions.ContainsKey(input.KeyCode))
            {
                LocalInputReceived(_playerInput.Actions[input.KeyCode], false);
            }
        }

        private void LocalInputKeyPress(KeyboardInput input)
        {
            if (_playerInput.Actions.ContainsKey(input.KeyCode))
            {
                LocalInputReceived(_playerInput.Actions[input.KeyCode], true);
            }
        }

        void LocalInputReceived(PlayerInput.Action action, bool pressed)
        {
            if(OnInputReceived != null)
                OnInputReceived(new PlayerInputCommand(action, pressed, PlayerInputCommand.SourceType.Local));
        }
    }
}